﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using System.Security.Cryptography.X509Certificates;
using System.IO;

using HostPc;

namespace HostPc
{

    namespace AWSIOTCoreSDK
    {
        /**
     * 调度当前设备与awsiotcore连接方面的实例化对象*/
        class Controller
        {

            public Configuration hostpc_config = null;
            public AWSIOTCoreSDK.Adapter awsiot_adapter = null;
            public AWSIOTCoreSDK.Application awsiot_app = null;

            /**
             * MqttContactAwsIOT_Controller构造函数**/
            public Controller(ref HostPc.Configuration __hostpc_config)
            {

                // 实例化MqttContactAwsIOT的三个对象
                hostpc_config = __hostpc_config;
                awsiot_adapter = new AWSIOTCoreSDK.Adapter(ref __hostpc_config);
                awsiot_app = new AWSIOTCoreSDK.Application();

            } // Controller()


            /**
             * 初始化Controller**/
            public void PassHVGController(ref HVGSerial.Controller hvg_controller_object)
            {
                // 传入hvg_controller实例化对象
                awsiot_adapter.hvg_controller = hvg_controller_object;

            } // PassHVGAdapter()


            /**
             * 开式运行MqttContactAwsIOT项目**/
            public void Run()
            {

                // 若没有连接上AWSIOT或者没有连接上HVG，则重连
                if (!awsiot_adapter.mqtt_client.IsConnected)
                {
                    awsiot_adapter.ReConnectAWSIOTCore();
                }
                if (!awsiot_adapter.hvg_controller.hvg_adapter.hvg_serial_port.IsOpen)
                {
                    awsiot_adapter.hvg_controller.hvg_adapter.ReconnectHVGSerial();
                } // if()

                // 订阅主题
                awsiot_adapter.Subscribe(ref hostpc_config.topic_sub);

                // 实现AWSIOTCore重连机制
                Thread __detect_connect_awsiotcore_thread = new Thread(DetectConnectAWSIOTCore);
                __detect_connect_awsiotcore_thread.Start();

            } // run()


            /**
             * 检查当前是否连接上AWSIOTCore**/
            public void DetectConnectAWSIOTCore()
            {
                while (true) {

                    if (awsiot_adapter.mqtt_client == null)
                    {
                        Console.WriteLine("AWSIOTCore中MQTT没有实例化，重连中");
                        awsiot_adapter.ReConnectAWSIOTCore();
                        Thread.Sleep(1000);
                        continue;
                    }
                    else {
                        if (!awsiot_adapter.mqtt_client.IsConnected)
                        {
                            Console.WriteLine("AWSIOTCore未连接上，重连中");
                            awsiot_adapter.ReConnectAWSIOTCore();
                            Thread.Sleep(1000);
                        }
                        else
                        {
                            Console.WriteLine("AWSIOTCore已连接 in DetectConnectAWSIOTCore()");
                            Thread.Sleep(5000);
                        }
                    }
                    
                } // while()

            } // DetectConnectAWSIOTCore（）

        } // class MqttContactAwsIOT_Controller


    } // namespace AWSIOTCoreSDK

} // namespace HostPc
